// packageH/income/ShareholderReward/Shareholder_reward.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    allData: {},
    member: {},
    rewards: {},
    name: {},
    rewards_type: 1,
    recordsList: [],
    amount_id: "",
    not_rewarded: "",
    rewarded: "",
    page: 1,
    language: "",
    shareholderShowDetailsIndex: null,
    sameLevelShowDetailsIndex: null,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
    this.getStatistic();
    this.getData();
  },
  initData() {
    this.data.recordsList = [];
    this.data.page = 1;
    this.data.total_page = 0;
    this.data.isLoadMore = true;
    this.data.amount_id = "";
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data.page);
    if (this.data.page >= this.data.total_page) {
      console.log("没有更多");
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages,
      });
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    });
  },
  tabHandel(index) {
    this.data.rewards_type = index.detail.index + 1;
    this.initData();
    this.getData();
    this.setData({
      shareholderShowDetailsIndex: null,
      sameLevelShowDetailsIndex: null,
    });
  },
  getData() {
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.partner-reward.frontend.controllers.reward-log.getLogByType"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        page: that.data.page,
        reward_type: that.data.rewards_type,
      },
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          that.setData({
            recordsList: res.data.data,
            not_rewarded: res.data.not_rewarded,
            rewarded: res.data.rewarded,
            total_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getMoreData() {
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.partner-reward.frontend.controllers.reward-log.getLogByType"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        page: that.data.page,
        reward_type: that.data.rewards_type,
      },
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          that.data.recordsList = that.data.recordsList.concat(res.data.data);
          that.setData({
            recordsList: that.data.recordsList,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getStatistic() {
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.partner-reward.frontend.controllers.reward-log.index"
    );
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          wx.setNavigationBarTitle({
            title: res.data.name,
          });
          that.setData({
            rewards: res.data,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  showDetails(event) {
    let index = event.currentTarget.dataset.index;
    const tab = event.currentTarget.dataset.tab;
    let showDetailsIndex = null;
    let key = "";
    if (tab === "shareholder") {
      showDetailsIndex = this.data.shareholderShowDetailsIndex;
      key = "shareholderShowDetailsIndex";
    } else {
      showDetailsIndex = this.data.sameLevelShowDetailsIndex;
      key = "sameLevelShowDetailsIndex";
    }
    if (showDetailsIndex == index) {
      index = null;
    }
    console.log(key);
    this.setData({
      [key]: index,
    });
  },
});
